package com.designer.shapes
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.events.Event;

	public class GeometricShape extends Sprite
	{
		
		private var _shape:String;
		private var _xStart:Number;
		private var _yStart:Number;
		private var _wid:Number;
		private var _hei:Number;
		private var _xFinal:Number;
		private var _yFinal:Number;
		private var _lineColor:Number;
		private var _lineThickness:Number;
		private var _lineAlpha:Number;
		private var _fillColor:Number;
		private var _fillAlpha:Number;
		//private var textField:TextField = new TextField();

		public function GeometricShape(shape:String, lineColor:Number, lineThickness:Number, lineAlpha:Number, fillColor:Number, fillAlpha:Number, xStart:Number, yStart:Number, xFinal:Number, yFinal:Number) {
			_shape = shape;
			convertPos(xStart, yStart, xFinal, yFinal);
			_wid = calcWid();
			_hei = calcHei();
			_lineColor = lineColor;
			_lineThickness = lineThickness;
			_lineAlpha = lineAlpha;
			_fillColor = fillColor;
			_fillAlpha = fillAlpha;
			/* textField.textColor = 0xFFFFFF;
			textField.autoSize = TextFieldAutoSize.LEFT;
			addChild(textField); */
			//this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener("addArrowActions", addArrowActions);
			this.addEventListener("removeArrowActions", removeArrowActions);
			this.addEventListener("stopDrag", onStopDrag);
		}
		
		public function draw():void {
			//To be overridden in the subclasses
		}
		
		public function change(xStart:Number, yStart:Number, xFinal:Number, yFinal:Number):void {
			convertPos(xStart, yStart, xFinal, yFinal);
			_wid = calcWid();
			_hei = calcHei();
			//textField.text += String(Math.round(_wid)) + "," + String(Math.round(_hei)) + "\n" + String(Math.round(_xStart)) + " , " + String(Math.round(_yStart))+ " , " + String(Math.round(_xFinal))+ " , " + String(Math.round(_yFinal));
		}
		
		private function onMouseDown(evt:MouseEvent):void {
			// limits dragging to the area inside the canvas
			/* var boundsRect:Rectangle = this.parent.getRect(this.parent);
			boundsRect.width -= this.size;
			boundsRect.height -= this.size; */
			this.startDrag();
			//adds the event listener for dragging shapes.. this way the shape movement will appear a lot smoother
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function onMouseUp(evt:MouseEvent):void {
			this.stopDrag();
			//removes the event listener for dragging shapes
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function onMouseMove(event:MouseEvent):void {
			event.updateAfterEvent();
		}
		
		public function addArrowActions(event:Event):void {
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		public function removeArrowActions(event:Event):void {
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		public function onStopDrag(event:Event):void {
			this.stopDrag();
		}
		
		public function convertPos(xStart:Number, yStart:Number, xFinal:Number, yFinal:Number):void {
			//4 cases to assure that always the xStart and yStart are in the upper left corner and xFinal and yFinal are in the lower right corner
			switch (_shape){
				case "line": 
						_xStart = xStart;
						_yStart = yStart;
						_xFinal = xFinal;
						_yFinal = yFinal;
						break;
				default:
					if(xStart > xFinal){
						//case 1
						if(yStart > yFinal){
							//textField.text = 1 + "\n";
							_xStart = xFinal;
							_xFinal = xStart;
							_yStart = yFinal;
							_yFinal = yStart;
						}
						//case 3
						else {
							//textField.text = 3 + "\n";
							_xStart = xFinal;
							_xFinal = xStart;
							_yStart = yStart;
							_yFinal = yFinal;
						}
					}
					else {
						//case 2
						if(yStart > yFinal){
							//textField.text = 2 + "\n";
							_xStart = xStart;
							_xFinal = xFinal;
							_yStart = yFinal;
							_yFinal = yStart;
						}
						//case 4
						else {
							//textField.text = 4 + "\n";
							_xStart = xStart;
							_xFinal = xFinal;
							_yStart = yStart;
							_yFinal = yFinal;
						}
					}
					break;
			}
		}
		
		public function calcWid():Number {
			var newWid:Number = _xFinal - _xStart;
			return newWid;
		}
		
		public function calcHei():Number {
			var newHei:Number = _yFinal - _yStart;
			return newHei;
		}

		public function get shape():String {
			return _shape;
		}
		
		public function get lineColor():Number {
			return _lineColor;
		}
		
		public function set lineColor(value:Number):void {
			_lineColor = value;		
		}
		
		public function get lineThickness():Number {
			return _lineThickness;
		}
		
		public function set lineThickness(value:Number):void {
			_lineThickness = value;
		}
		
		public function get lineAlpha():Number {
			return _lineAlpha;
		}
		
		public function set lineAlpha(value:Number):void {
			_lineAlpha = value;
		}
		
		public function get fillColor():Number {
			return _fillColor;
		}
		
		public function set fillColor(value:Number):void {
			_fillColor = value;
		}
		
		public function get fillAlpha():Number {
			return _fillAlpha;
		}
		
		public function set fillAlpha(value:Number):void {
			_fillAlpha = value;
		}
		
		public function get xStart():Number {
			return _xStart;
		}
		
		public function set xStart(value:Number):void {
			_xStart = value;
		}
		
		public function get yStart():Number {
			return _yStart;
		}
		
		public function set yStart(value:Number):void {
			_yStart = value;
		}
		
		public function get wid():Number {
			return _wid;
		}
		
		public function set wid(value:Number):void {
			_wid = value;
		}
		
		public function get hei():Number {
			return _hei;
		}
		
		public function set hei(value:Number):void {
			_hei = value;
		}
		
		public function get xFinal():Number {
			return _xFinal;
		}
		
		public function set xFinal(value:Number):void {
			_xFinal = value;
		}
		
		public function get yFinal():Number {
			return _yFinal;
		}
		
		public function set yFinal(value:Number):void {
			_yFinal = value;
		}
		
		
	}
}